<?php
namespace Home\Controller;
use Think\Controller;
use Org\Util\Date;
use ZipArchive;
class IndexController extends Controller {
    public function __construct()
    {
        parent::__construct();
        $pub_va=A('Public');
        $pub_va->login_check();
    }
    public function index(){
        //redirect("http://www.baidu.com?a=1");
        $plug=D('Plug');
        //$result=$plug->select();
        //dump($result);
        $this->display();        
    }
    public function plug_add(){
        $this->display();
    }
    public function plug_add_process(){
        $data=I("post.");
        //dump($data);
        $data['add_time']=time();
        $config = array(
            'maxSize' => 102400000, 
            'rootPath' => './plug/', 
            'exts' => array('zip','sql'), 
            'autoSub' => false,
            'saveName'      => $data['plug_name'],
            'replace'  =>  true,
        );
        $data['plug_home']=$data['plug_home'];
        $data['plug_admin']=$data['plug_admin'];
        if($_FILES['file']['size']){
            $upload = new \Think\Upload($config);// 实例化上传类
            $info   =   $upload->upload();
            //dump($info);
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功
                $path=$_SERVER['DOCUMENT_ROOT'] . __ROOT__ .'/plug/';
                $file_zip=$path.$info['file']['savename'];
                $zip = new ZipArchive;
                $res = $zip->open($file_zip);
                if ($res === TRUE) {
                    //解压缩到$path文件夹
                    $zip->extractTo($path);
                    $zip->close();
                    $result = @unlink ($file_zip);
                    //$arr= explode(".",$info['file']['name']);
                    //$namedir=$arr[0];
                    //$base_path=$_SERVER['HTTP_HOST'].__ROOT__.'/plug';
                    $data['plug_home']=$data['plug_home'];
                    $data['plug_admin']=$data['plug_admin'];
                    //echo $data['plug_home'];
                    //
                } else {
                    $this->error('插件安装失败！');
                }
            }
        }
        $data['add_user']='1';
        //dump($data);
        $plug=D('Plug');
        $result=$plug->add($data);
        if($info['filesql']){
            $file_sql = $_SERVER['DOCUMENT_ROOT'] . __ROOT__ . "/plug/".$info['filesql']['savename'];
            $re_sql=$plug->install_sql($file_sql);
        }
        if($result){
            $this->success('操作成功', 'Index/index');
        }
    }
}